package com.keepsoft.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Font;

/**
 * @author liuChengWen.
 * @date 2018\7\3 0003 15:03
 * @desc
 */
public class AnalysisPDF {

    /**
     * 解析PDF文件
     * @param fileName
     * @return
     * @throws IOException
     */
    public String AnalySis(File fileName) throws IOException {
        PDDocument document=PDDocument.load(fileName);
        // 获取页码
        int pages = document.getNumberOfPages();

        // 读文本内容
        PDFTextStripper stripper=new PDFTextStripper();
        // 设置按顺序输出
        stripper.setSortByPosition(true);
        stripper.setStartPage(1);
        stripper.setEndPage(pages);
        String content = stripper.getText(document);
        return content;
    }

    /**
     * 根据文本生成pdf
     * @param fileName
     * @param text
     * @throws Exception
     */
    public void createPDF(File fileName,String text) throws Exception{
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
        document.open();
        Font f1 = FontFactory.getFont("simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        document.add(new Paragraph(text, f1));
        document.close();
        writer.close();
    }
}
